package cn.buyclss.backend.service.impl.shop;

import cn.buyclss.backend.entity.domain.User;
import cn.buyclss.backend.entity.domain.shop.Goods;
import cn.buyclss.backend.entity.domain.shop.GoodsExample;
import cn.buyclss.backend.entity.domain.shop.GoodsSpecs;
import cn.buyclss.backend.entity.domain.shop.GoodsSpecsExample;
import cn.buyclss.backend.entity.po.shop.ShopPO;
import cn.buyclss.backend.entity.support.RespResult;
import cn.buyclss.backend.entity.vo.shop.GoodsSpecsVO;
import cn.buyclss.backend.mapper.shop.GoodsMapper;
import cn.buyclss.backend.mapper.shop.GoodsSpecsMapper;
import cn.buyclss.backend.mapper.shop.ShopMapper;
import cn.buyclss.backend.service.shop.GoodsSpecsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Cecurio
 * @create: 2019-03-19 15:37
 **/
@Service
public class GoodsSpecsServiceImpl implements GoodsSpecsService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsSpecsMapper goodsSpecsMapper;

    @Override
    public RespResult addOne(GoodsSpecsVO goodsSpecsVO) {
        RespResult res = RespResult.successInstance();

        GoodsSpecs goodsSpecs = new GoodsSpecs();
        // 1、获取商店编号
        Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        Integer uid = 0;
        if (obj instanceof User) {
            User user = (User) obj;
            uid = user.getId();
        } else {
            return RespResult.failureInstance("系统异常，稍后重试！");
        }

        List<ShopPO> shops = shopMapper.selectUserShop(uid);
        if (shops.size() < 1) {
            return RespResult.failureInstance("您还没有商铺，无法添加！");
        }
        Integer shopId = shops.get(0).getId();

        // 设置shop_id
        goodsSpecs.setShopId(shopId);

        // 设置前端传过来的参数
        goodsSpecs.setGoodsId(goodsSpecsVO.getGoodsId());
        goodsSpecs.setSpecValue(goodsSpecsVO.getSpecValue());
        goodsSpecs.setPrice(goodsSpecsVO.getPrice());
        goodsSpecs.setStockNum(goodsSpecsVO.getStockNum());
        // 设置默认值
        goodsSpecs.setCreatedBy(uid);
        goodsSpecs.setUpdatedBy(uid);
        goodsSpecs.setCreatedTime(new Date());
        goodsSpecs.setUpdatedTime(new Date());

        // 插入到规格表
        goodsSpecsMapper.insertSelective(goodsSpecs);

        Integer goodsId = goodsSpecsVO.getGoodsId();

        // 通过goodsId查goods表
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andIdEqualTo(goodsId);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);

        if (goodsList == null || goodsList.size() < 1) {
            return RespResult.failureInstance("没有该种商品");
        }

        Goods currentGoods = goodsList.get(0);

        // 通过goodsId查goodsSpecs表
        GoodsSpecsExample goodsSpecsExample = new GoodsSpecsExample();
        GoodsSpecsExample.Criteria goodsSpecsExampleCriteria = goodsSpecsExample.createCriteria();
        goodsSpecsExampleCriteria.andGoodsIdEqualTo(goodsId);
        List<GoodsSpecs> goodsSpecsList = goodsSpecsMapper.selectByExample(goodsSpecsExample);
        long minPrice = goodsSpecsVO.getPrice();
        Integer sumStockNum = goodsSpecsVO.getStockNum();
        for (int i = 0; i < goodsSpecsList.size(); i++) {
            GoodsSpecs temp=goodsSpecsList.get(i);
            sumStockNum += temp.getStockNum();
            if (temp.getPrice() < minPrice) {
                minPrice = goodsSpecsList.get(i).getPrice();
            }
        }

        Goods updateGoods=new Goods();
        updateGoods.setId(goodsId);
        updateGoods.setStockNum(sumStockNum);
        updateGoods.setPrice(minPrice);
        updateGoods.setPromotionPrice(minPrice);
        // 更新库存，价格
        goodsMapper.updateByPrimaryKeySelective(updateGoods);
        return res;
    }
}
